<template>
  <div>
    <el-row v-if="hasMsg"
            v-for="(msg,index) in msgDataList"
            :key="index" style="margin-top: 20px">
      <div>
        <el-col :span="2" :offset="msg.offset+1">
          <div class="chat-avatar" v-if="msg.nickName">
            {{msg.nickName}}
          </div>
        </el-col>
        <el-col :span="20 - msg.offset" class="chat-msg-box">
            <div>
              <span  class="chat-dept-name">{{msg.orgName}}</span>
              <span class="chat-msg-time">{{msg.msgTime}}</span>
            </div>
            <div>
            {{msg.msgContent}}
            <file-download :file-id="msg.fileId" :file-name="msg.fileFullName"></file-download>
            </div>
        </el-col>
      </div>
    </el-row>

    <!--  没有消息-->
    <div v-if="!hasMsg" style="text-align: center;font-size: 18px;color: #3333;height: 300px;line-height: 300px;">
      暂无数据
    </div>
    <!--  分页组件-->
    <el-row v-if="hasMsg">
      <el-col :span="24" offset="1">
        <el-pagination
          @size-change="sizeChange"
          @current-change="currentPage"
          :current-page="page.currentPage"
          :page-size="page.pageSize"
          :total="page.total"
          :page-sizes="[10, 20, 30, 40 ,50 ,100]"
          layout="total, sizes, prev, pager, next, jumper"
          v-if="hasMsg"
          class="paginationcss">
        </el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import {messagePage} from '@/api/planning/message';
  import fileDownload from "@/components/file/download.vue";

  export default {
    name: "msgDetail",
    components: {fileDownload},
    data: function () {
      return {
        opinion:'',
        dialogVisible:false,
        hasMsg:false,
        msgDataList:[],//consultMsg组件内容？
        msgListtemp:[],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
      }
    },
    props:{
      topicId:Number
    },
    created:function(){
      this.getMessage();
    },
    watch:{
      msgDataList:function (val,oldVal) {
        this.hasMsg = (val != null && val.length > 0);
      }
    },
    methods: {
      currentPage(currentPage){
        this.page.currentPage = currentPage;
        this.getMessage();
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
        this.getMessage();
      },
      getMessage(){
        messagePage(Object.assign({
          current: this.page.currentPage,// 当前页数
          size: this.page.pageSize,// 每页显示多少条
          topicId:this.topicId,
          ascs:'send_time'
        })).then(response => {
          if (response.data.code == 0) {
            let records = response.data.data.records;
            this.page.currentPage = response.data.data.current;
            this.page.pageSize = response.data.data.size;
            this.page.total = response.data.data.total;
            this.putList(records);
          }
        });
      },
      putList(record){
        this.msgListtemp=[];
        this.recursive(record);    //递归消息放入消息列msgListtemp中
        this.msgDataList = this.msgListtemp;
      },
      recursive(record){
        for(let message of record){
          let level = 0;
          if(message.parentId != null){
            level = 1;
          }
          let msgDataMap = {
            orgName:message.senderNickname,
            nickName:message.senderNickname?message.senderNickname.substring(0,2):'',
            msgTime:message.sendTime,
            msgContent:message.content,
            senderId:message.senderId,
            topicId:message.topicId,
            messageId:message.messageId,
            offset:level*2,//消息框，偏移量
            fileId:message.fileId,
            fileFullName:message.fileFullName,
          };
          this.msgListtemp.push(msgDataMap);
          if (message.children !== null) { //当还有子消息时继续递归
            this.recursive(message.children);
          }
        }
      },
      cancel() {
        this.dialogVisible = false;
      },
    }
  };

</script>

<style lang="scss" scoped>

  .chat-avatar{
    border-radius: 30px;
    width: 60px;
    height: 60px;
    line-height: 60px;
    text-align: center;
    background-color: rgb(45, 140, 240);
    color: #fff;
    float: left;
  }

  .chat-msg-time{
    display: block;
    margin-bottom: 2px;
    font-size: 14px;
    color: #999;
    float: right;
  }

  .chat-dept-name{
    font-weight: 600;
    font-size: 14px;
    display: block;
    margin-bottom: 2px;
    font-size: 14px;
    float: left;
  }

  .chat-msg-box{
    padding: 15px 25px;
    border: 1px solid #ccc;
    border-radius: 10px;
  }

  .chat-msg-box div{
    float: left;
    width: 100%;
    margin-bottom: 10px;
  }

  .paginationcss {
    margin-top: 20px;
    float: right;
    margin-right: 28px;
  }
</style>
